package com.tenbit.com.vo;

import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.FileCopyUtils;
import org.springframework.web.servlet.view.AbstractView;

public class DownLoadView extends AbstractView{
	@Override
	protected void renderMergedOutputModel(Map<String, Object> model,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		File file=(File)model.get("downloadFile");
	
		
		String userAgent=request.getHeader("User-Agent");
		boolean ie=userAgent.indexOf("MSIE")>-1;		
		String fileName=null;
		if(ie){//IE
			fileName=URLEncoder.encode(file.getName(),"UTF-8");
		}else{
			fileName=new String(file.getName().getBytes("UTF-8"));
		}
		response.setHeader("Content-Disposition", "attachment;filename=\""+fileName+"\";");
		response.setHeader("Content-Transfer-Encoding", "binary");
		response.setContentType(this.getContentType());		
		response.setContentLength((int)file.length());
		
		OutputStream out=response.getOutputStream();
		FileInputStream fis=null;
		try {
			fis=new FileInputStream(file);
			FileCopyUtils.copy(fis, out);
			
		} catch (Exception e) {
			// TODO: handle exception
		}
		out.flush();
			
	}
}
